/**
 * @param value
 * @return {Boolean}
 * @description 判断是否为空
 * */
export const isEmpty = (value) => {
	return (
		value === undefined ||
		value === null ||
		(typeof value === 'object' && Object.keys(value).length === 0) ||
		(typeof value === 'string' && value.trim().length === 0)
	)
}

/**
 * @param string
 * @param noAddBase
 * @return {String}
 * @description 添加URL地址+自动去除第一斜杠
 * */
export const addBaseURL = (string, noAddBase) => {
	if (string && string !== '') {
		if (string.charAt(0) !== '/' && string.charAt(0) !== '\\') {
			string = '/' + string
		}
		if (string.charAt(string.length - 1) === '/') {
			string = string.substr(0, string.length - 1)
		}
		if (noAddBase === true) {
			return string
		} else {
			return string
		}
	} else {
		return ''
	}
}

/**
 * @param html
 * @return {String}
 * @description 过滤富文本HTML标签
 * */
export const getSimpleHtml = (html = '') => {
	html = html.replace('↵', '')
	const re1 = new RegExp('<.+?>', 'g') // 匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
	const msg = html.replace(re1, '') // 执行替换成空字符
	return msg
}

/**
 * @description 过滤时间
 * @return {String}
 **/
export const formatDateTime = (value) => {
	const date = value ? (new Date(value.replace(/-/g, '/'))) : (new Date())
	const year = date.getFullYear()
	const month = (date.getMonth() + 1).toString().padStart(2, '0')
	const day = date
		.getDate()
		.toString()
		.padStart(2, '0')
	const hours = date
		.getHours()
		.toString()
		.padStart(2, '0')
	const minutes = date
		.getMinutes()
		.toString()
		.padStart(2, '0')
	const seconds = date
		.getSeconds()
		.toString()
		.padStart(2, '0')
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

/**
 * @author 王业鹏
 * @param {Object} _this 组件this
 * @param {String|Object} key 重置对象的键
 * @param {String|Object } ref 表单的ref
 * @description 重置表单
 **/
export const resetForm = (_this, key = null, ref = 'uForm') => {
	return new Promise(resolve => {
		if (ref) {
			_this.$refs[ref].resetFields();
		}
		_this.$nextTick(() => {
			if (key) Object.assign(_this.$data[key], _this.$options.data()[key])
			else Object.assign(_this.$data, _this.$options.data());
			resolve()
		})
	})
};

/**
 * @author 王业鹏
 * @param {Object} _this 组件this
 * @param {String|Object} key 重置对象的键
 * @description 重置组件
 **/
export const resetData = (_this, key = null) => {
	return new Promise(resolve => {
		if (key) Object.assign(_this.$data[key], _this.$options.data()[key])
		else Object.assign(_this.$data, _this.$options.data());
		resolve()
	})
};
